import { authorizeParams, getUserInfoParams, loadParams, loginParams, MiniGameApiBase, ModalParams, recordedGameScreenParams, toastParams } from "../miniGameBase/miniGameApiBase";


//TODO 未完成，需要查看Api文档

const huaWei: any = window['tt'];
export default class HuaWeiApi extends MiniGameApiBase {




    protected m_AppId: string = "";
    protected m_Secret: string = "";

    protected m_VideoAdIdIndex: number = 0;
    protected m_VideoAdIdList: string[] = [];

    protected m_BannerAdIdIndex: number = 0;
    protected m_BannerAdId: string[] = [];

    protected m_InsertAdIdIndex: number = 0;
    protected m_InsertAdId: string[] = [];


    protected m_videoAd: any = undefined;
    protected m_BannerAd: any = undefined;



    validAppId(): void {
        console.log("oppo-api-validAppId");
    }

    ready(): void {
        console.log("huaWei-api-ready");
    }

    getUserInfo(params: getUserInfoParams): void {
        huaWei.getUserInfo(params);
    }

    showToast(params: toastParams): void {
        huaWei.showToast(params);
    }

    showLoading(params: loadParams): void {
        huaWei.showLoading(params);
    }

    hideLoading(): void {
        huaWei.hideLoading();
    }

    showModal(params: ModalParams): void {
        huaWei.showModal(params);
    }

    authorize(params: authorizeParams): void {
        params.success && params.success();
    }

    login(params: loginParams): void {
        huaWei.login(params);
    }



    getVideoAdId(): string {
        const id: string = this.m_VideoAdIdList[this.m_VideoAdIdIndex];
        this.m_VideoAdIdIndex++;
        if (this.m_VideoAdIdIndex >= this.m_VideoAdIdList.length) {
            this.m_VideoAdIdIndex = 0;
        }
        return id;
    }

    loadAndShowVideoAd(successCB: () => void, errorCB: (error: any) => void, completeCB?: () => void, reportAnalyticsExplain?: string): void {
        this.showLoading({ title: '请求中', mask: true });

        const videoAdId = this.getVideoAdId();
        console.log('tt 视频广告', videoAdId, reportAnalyticsExplain);

        //创建 视频
        const videoAd: any = huaWei.createRewardedVideoAd({
            adUnitId: videoAdId,
        });

        //监听 视频关闭
        let onCloseVideoAd = (res) => {
            //
            videoAd.offClose(onCloseVideoAd);
            console.log('tt 视频广告关闭', res);
            if (res && res.isEnded) {
                // 给予奖励
                successCB();
            } else {
                errorCB('视频中途退出');
            }
            completeCB && completeCB();
        };

        //重试
        let onTryAgain = (e: any) => {
            videoAd.load().then(() => {
                videoAd.show().then(() => {
                    this.hideLoading();
                    videoAd.onClose(onCloseVideoAd);
                }).catch((e) => {//展示异常
                    errorCB && errorCB('视频展示异常');
                    completeCB && completeCB();
                });
            }).catch((e) => {//加载异常
                errorCB && errorCB('视频拉取异常02');
                completeCB && completeCB();
            });
        }

        console.log('展示一次');
        videoAd.show().then(() => {
            this.hideLoading();
            videoAd.onClose(onCloseVideoAd);
            //展示异常
        }).catch((e) => {
            console.log('重试一次');
            onTryAgain(e);
        })
    }



    getInsertAdId(): string {
        const id: string = this.m_InsertAdId[this.m_InsertAdIdIndex];
        this.m_InsertAdIdIndex++;
        if (this.m_InsertAdIdIndex >= this.m_InsertAdId.length) {
            this.m_InsertAdIdIndex = 0;
        }
        return id;
    }

    showInsertAd(onAdClose: () => void, onFailed: () => void): void {

        console.log("加抖音 插屏 广告");

        const adId = this.getInsertAdId();
        let insertAd = huaWei.createInterstitialAd({
            adUnitId: adId,
        });

        insertAd.load();
        insertAd.onLoad(() => {
            console.warn('插屏广告 加载完成');

            insertAd.show().catch((err) => {
                console.warn('插屏广告 显示失败 :' + JSON.stringify(err));
                insertAd.destroy();
                insertAd = null;
                if (onFailed) {
                    onFailed();
                }
            });
        });

        insertAd.onError((err) => {
            console.warn('插屏广告 加载失败' + JSON.stringify(err));
            insertAd.destroy();
            insertAd = null;
            if (onFailed) {
                onFailed();
            }
        });

        insertAd.onClose(() => {
            console.warn('插屏广告 关闭');
            if (insertAd != null) {
                insertAd.destroy();
                insertAd = null;
            }
            if (onAdClose) {
                onAdClose();
            }
        });
    }

    getBannerAdId(): string {
        const id: string = this.m_BannerAdId[this.m_BannerAdIdIndex];
        this.m_BannerAdIdIndex++;
        if (this.m_BannerAdIdIndex >= this.m_BannerAdId.length) {
            this.m_BannerAdIdIndex = 0;
        }
        return id;
    }

    showBannerAD(node: cc.Node, successCB?: () => void, errorCB?: (error: any) => void, onCloseCB?: () => void,
        setBannerPos?: ({ screenWidth, screenHeight }: { screenWidth: any; screenHeight: any; }) => cc.Vec2): void {

    }

    hideBanner() {
        // if (this.m_BannerAd != null) {
        //     this.m_BannerAd.hide();
        // }
    }


    recordedGameScreenStart(params: recordedGameScreenParams, onStart?: (res: any) => void): void {
        console.log("huaWei-api-recordedGameScreenStart");
        onStart && onStart(null);
    }
    recordedGameScreenStop() {
        console.log("huaWei-api-recordedGameScreenStop");
        return true;
    }
    shareRecordedGameScreen(successCb?: () => void, failCb?: (e?: any) => void): void {
        console.log("huaWei-api-shareRecordedGameScreen");
        successCb && successCb();
    }
    isHaveRecordedGameScreen(): boolean {
        return true;
    }

}